Skip to content

Fix test isolation issues with keyring storage#47

Merged
cevian merged 2 commits intomainfrom
fix-test-isolation
Oct 8, 2025
Merged

Fix test isolation issues with keyring storage#47
cevian merged 2 commits intomainfrom
fix-test-isolation

Conversation

@cevian
Copy link
Contributor

@cevian cevian commented Oct 7, 2025

Summary

  • Fixes test failures when running go test ./... -count=1 due to keyring race conditions
  • Tests from different packages were competing for the same keyring service name
  • Implemented unique keyring namespaces per test using t.Name()

Problem

Tests were failing when run without cache (-count=1) because:

  • All tests shared the same "tiger-cli-test" keyring service name
  • When tests from different packages ran in parallel, they would interfere with each other
  • This caused random failures where tests would get wrong API keys or fail to find expected values

Solution

Implemented SetTestServiceName(t *testing.T) function that:

  • Creates unique keyring service names based on test name (e.g., "tiger-test-TestAuthLogin")
  • Automatically registers cleanup via t.Cleanup() - no manual cleanup needed
  • Prevents race conditions when tests run in parallel across packages

Changes

  • api_key.go: Added SetTestServiceName() with automatic cleanup
  • 7 test files: Updated to call SetTestServiceName(t) in setup functions
  • Removed all manual ResetTestServiceName() calls - cleanup is now automatic

Test Plan

  • Run go test ./... -count=1 - all tests pass
  • Run go test ./... -count=3 - tests pass consistently
  • Run tests with parallel packages (default) - no race conditions
  • Verify keyring isolation by checking unique service names per test

🤖 Generated with Claude Code

This fixes test failures when running `go test ./... -count=1` caused by
tests from different packages racing to use the same keyring service name.

Solution implemented:
- Added SetTestServiceName() function that creates unique keyring service
  names based on t.Name()
- Each test gets its own isolated keyring namespace (e.g., "tiger-test-TestAuthLogin")
- Automatic cleanup via t.Cleanup() - no manual cleanup needed
- Prevents race conditions when tests run in parallel across packages

Key changes:
- api_key.go: Added SetTestServiceName() with automatic cleanup
- All test files: Call SetTestServiceName(t) in setup functions
- Removed manual ResetTestServiceName() calls - cleanup is automatic

This ensures complete test isolation without requiring `-p 1` flag.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cevian cevian requested a review from nathanjcochran October 7, 2025 10:40
Copy link
Member

@nathanjcochran nathanjcochran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Can you also remove the -p 1 flag from the go test invocation in the GitHub Actions workflow? That's how I was working around this issue before in CI 😅 (i.e. by just running all of the tests sequentially, rather than the default behavior of running tests in different packages in parallel).

Comment on lines +37 to +48
// SetTestServiceName sets a unique service name for testing based on the test name
// This allows tests to use unique service names to avoid conflicts when running in parallel
// The cleanup is automatically registered with t.Cleanup()
func SetTestServiceName(t *testing.T) {
testServiceNameOverride = "tiger-test-" + t.Name()

// Automatically clean up when the test finishes
t.Cleanup(func() {
testServiceNameOverride = ""
})
}

Copy link
Member

@nathanjcochran nathanjcochran Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One potential worry I have with this approach is that there could be race conditions where multiple parallel tests call SetTestServiceName concurrently, thereby overwriting the value of testServiceNameOverride set by the other tests.

I guess that's not an issue right now, because we don't have any parallel tests within the same package, and tests in different packages are fully isolated from each other (i.e. they don't share memory or this testServiceNameOverride variable). Right?

Still, it seems like a potential gotcha for down the road. I feel like it would be better to somehow pass the keyring service name into the calls that access the keyring (even though that would be annoying in many ways).

In any case, I think this is fine for now. Definitely an improvement over what we had 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this needs a better refactor but merging for now

The test isolation fix makes this workaround unnecessary. Tests can now run in parallel without race conditions.
@cevian cevian merged commit 56de6a5 into main Oct 8, 2025
2 checks passed
@cevian cevian deleted the fix-test-isolation branch October 8, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants